package com.hna.face.bussiness;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hna.face.config.SysConfig;
import com.hna.face.model.FaceRecognizeResult;
import com.hna.face.model.FaceVehicle;
import com.hna.face.service.IFaceRecognizeResultService;

import freemarker.template.utility.StringUtil;

@Service
public class DataMessageHandler extends MessageHandler {

	private final static Logger log = LoggerFactory.getLogger(DataMessageHandler.class);

	@Autowired
	private IFaceRecognizeResultService service;

//	@Value("{$recognizeLabphoto_dir}")
//	public  String recognizeLabphotoDir;
	
	@Override
	public void run() {
		if (message == null) {
			return;
		}
		if(service==null) {
			log.info("service is null");
		}
		
		log.info("1");
		byte[] payload = message.getPayload();// 获取二进制文件
		log.info("2");
		String resultString = new String(payload);
		log.info("3");
		//resultString.substring(resultString.indexOf("data:"));
		//log.info("String2"+resultString);
		JSONObject json=null;
		try {
			log.info("4");
			json = JSON.parseObject(resultString);// 转成json对象
			log.info("5");
		}
		catch(Exception e)
		{
			log.error("",e);
		}
//		JSONObject 
		
		FaceRecognizeResult result = new FaceRecognizeResult();
		log.info("6");
		result.setVehicleNo(json.get("vehicleNo").toString());
		log.info("7");
		log.info("vehicleNo:"+json.get("vehicleNo").toString());
		
		if(json.get("vehicleNo")!=null || json.get("vehicleNo")!="") {
			result.setScheduleNo(json.get("vehicleNo").toString());
			log.info(json.get("vehicleNo").toString());
		}
		log.info(json.get("result").toString());
		Integer recognizeResult=Integer.valueOf(json.get("result").toString());
		log.info("recognizeResult:"+recognizeResult);
		result.setResult(recognizeResult);
		
		log.info("result:" + JSON.toJSONString(result));
		
		result.setPersonName(json.get("personName").toString());
		
		log.info("8");
		String photo = json.get("photo").toString();//获取photo名
		result.setPhoto(photo);
		//result.setDescr(json.get("descr").toString());
		result.setCreateTime(new Date());
		result.setUpdateTime(new Date());
		log.info("result:" + JSON.toJSONString(result));

		//放如数据库
		service.insert(result);
		
		//将获取到的图片放入同步文件夹
		String photoString = json.get("photoString").toString();
		
		byte[] img = Base64.decodeBase64(photoString);
		
		OutputStream out = null;
		try {
//			SysConfig.recognizeLabphotoDir
			log.info(SysConfig.recognizeLabphotoDir);
			log.info(SysConfig.recognizeLabphotoDir+photo);
			out = new FileOutputStream(SysConfig.recognizeLabphotoDir+photo);
			//out = new FileOutputStream(SysConfig.photoDir+photo);
			out.write(img);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error("DataPhotoOut is error" ,e);
		}
	}
}
